<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    try {
        $cli = new \Prediction\PredictionClient('220.228.175.3:11051',[
            'credentials' => \Grpc\ChannelCredentials::createInsecure()
        ]);
        print_r($cli);
        echo "\r\n";
    } catch (\Exception $e) {
        echo 1111 ."\r\n";
        echo $e->getFile() . "\r\n";
        echo $e->getLine() . "\r\n";
        echo $e->getMessage() . "\r\n";
    }
    echo 2222 ."\r\n";
    exit;
    echo "执行 一对一 方法...", '<br/>';
    $rq = new \Prediction\RequestParams();
    $rq->setUserId(1);
    $rq->setPostIds('1,2,3');
    list($rp, $status) = $cli->Sorting($rq)->wait();
    if (!$rp) {
        echo json_encode($status);
        return;
    }
    // echo "name: ".$rp->getName(), '<br/>';
    // 序列化为string
    echo $rp->serializeToJsonString(), '<br/>';
    return;
    echo "执行 一对多 方法...", '<br/>';
    $rq = new \Prediction\RequestParams();
    $rq->setCompanyName("B技术公司");
    $call = $cli->CompanyJobs($rq);
    $stream = $call->responses();
    foreach ($stream as $s) {
        //序列化为string
        echo $s->serializeToJsonString(), '<br/>';
    }

    echo "执行 多对一 方法...", '<br/>';
    $call = $cli->AnalysisSalary();
    for ($i = 0; $i < 3; $i++) {
        $sr = new \Protobuf\SalaryRange();
        $sr->setMin(10+$i);
        $sr->setMax(15+$i);
        $call->write($sr);
    }
    list($rp, $status) = $call->wait();
    echo $rp->serializeToJsonString(), '<br/>';

    echo "执行 多对多 方法...", '<br/>';
    $call = $cli->GetJobs();
    $rq = new \Prediction\RequestParams();
    for ($i = 10; $i < 14; $i++) {
        $rq->setId($i);
        $call->write($rq);
    }
    $call->writesDone();
    while ($rp = $call->read()) {
        //序列化为string
        echo $rp->serializeToJsonString(), '<br/>';
    }

    return $router->app->version();
});
